package com.yunzai.register.service.impl;

import com.yunzai.secure.exception.ServiceException;
import com.yunzai.register.domain.Register;
import com.yunzai.register.mapper.RegisterMapper;
import com.yunzai.register.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements IRegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public int register(Register register) {
        Register user = registerMapper.selectRegisterByUsername(register.getUsername());
        if (user != null) {
            throw new ServiceException("账户已存在！");
        } else if (register.getUsername() == null) {
            throw new ServiceException("用户名不能为空！");
        } if (register.getPassword() == null) {
            throw new ServiceException("密码不能为空！");
        }
        register.setPassword(passwordEncoder.encode(register.getPassword()));
        return registerMapper.insertRegister(register);
    }

}
